package com.homelink.android.explorerup.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.homelink.android.explorerup.R;
import com.homelink.android.explorerup.db.Bookmark;
import com.homelink.android.explorerup.db.DBUtil;
import com.homelink.android.util.App;

public class ContentMenuActivity extends Activity implements OnClickListener {
	public static final String CURRENT_FILE = "CURRENT_FILE";
	public static final String CURRENT_PATH = "CURRENT_PATH";
	public static final int OPERATOR_CUT = 1;
	public static final int OPERATOR_COPY = 2;
	public static final String TAG = "ContentMenuActivity";
	private Button mBtnCancel;
	private Button mBtnRename;
	private ViewFlipper mFlipper1;
	private ViewFlipper mFlipper2;
	private ViewFlipper mFlipper3;
	private ViewFlipper mDeleteFlipper1;
	private ViewFlipper mDeleteFlipper2;
	private Button mBtnDelete;
	private Button mBtnCancelRename;
	private Button mBtnConfirmRename;
	private EditText mEditRename;
	private Button mBtnCancelDelete;
	private Button mBtnConfirmDelete;
	private String mCurrentFile;
	private String mCurrentPath;
	private Button mBtnCut;
	private Button mBtnCopy;
	private Button mBtnPaste;
	private Button mBtnBookmark;

	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		this.setContentView(R.layout.contentmenu);

		mCurrentFile = this.getIntent().getStringExtra(CURRENT_FILE);
		if (mCurrentFile != null) {
			this.setTitle(this.getTitle() + " " + mCurrentFile);
			mCurrentPath = getIntent().getStringExtra(CURRENT_PATH);
		}

		mEditRename = (EditText) findViewById(R.id.editRenmae);

		mFlipper1 = (ViewFlipper) findViewById(R.id.switcherRename1);
		mFlipper2 = (ViewFlipper) findViewById(R.id.switcherRename2);
		mFlipper3 = (ViewFlipper) findViewById(R.id.switcherRename3);

		mDeleteFlipper1 = (ViewFlipper) findViewById(R.id.switcherDelete1);
		mDeleteFlipper2 = (ViewFlipper) findViewById(R.id.switcherDelete2);

		mBtnCancel = (Button) findViewById(R.id.btnCancel);
		mBtnCancel.setOnClickListener(this);

		mBtnRename = (Button) findViewById(R.id.btnRename);
		mBtnRename.setOnClickListener(this);

		mBtnCancelRename = (Button) findViewById(R.id.btnRejectRename);
		mBtnCancelRename.setOnClickListener(this);

		mBtnConfirmRename = (Button) findViewById(R.id.btnConfirmRename);
		mBtnConfirmRename.setOnClickListener(this);

		mBtnDelete = (Button) findViewById(R.id.btnDelete);
		mBtnDelete.setOnClickListener(this);

		mBtnCancelDelete = (Button) findViewById(R.id.btnRejectDelte);
		mBtnCancelDelete.setOnClickListener(this);

		mBtnConfirmDelete = (Button) findViewById(R.id.btnConfirmDelete);
		mBtnConfirmDelete.setOnClickListener(this);

		mBtnCut = (Button) findViewById(R.id.btnCut);
		mBtnCut.setOnClickListener(this);

		mBtnCopy = (Button) findViewById(R.id.btnCopy);
		mBtnCopy.setOnClickListener(this);

		mBtnPaste = (Button) findViewById(R.id.btnPaste);
		mBtnPaste.setOnClickListener(this);
		
		mBtnBookmark = (Button)findViewById(R.id.btnAddToFavori);
		mBtnBookmark.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onRestart");
		if (App.cutFile != null) {
			File f = new File(App.cutFile);
			if (f.exists()) {
				mBtnPaste.setVisibility(View.VISIBLE);
			} else {
				mBtnPaste.setVisibility(View.GONE);
				App.cutFile = null;
			}
		} else {
			mBtnPaste.setVisibility(View.GONE);
		}
	}

	public void onClick(View v) {
		Log.d(TAG, "onClick");
		if (mBtnCancel == v) {
			finish();

		} else if (mBtnRename == v) {
			mFlipper1.showNext();
			mFlipper2.showNext();
			mFlipper3.showNext();
			Log.d(TAG, "onClick: showNext view");
		} else if (mBtnCancelRename == v) {
			mFlipper1.showPrevious();
			mFlipper2.showPrevious();
			mFlipper3.showPrevious();
		} else if (mBtnConfirmRename == v) {
			Editable tmp = mEditRename.getText();
			if (tmp == null || tmp.toString().trim().equals("")) {
				Animation ani = AnimationUtils
						.loadAnimation(this, R.anim.shake);
				mEditRename.startAnimation(ani);
			} else {
				rename();
			}
		} else if (mBtnDelete == v) {
			mDeleteFlipper1.showNext();
			mDeleteFlipper2.showNext();

		} else if (mBtnCancelDelete == v) {
			mDeleteFlipper1.showPrevious();
			mDeleteFlipper2.showPrevious();
		} else if (mBtnConfirmDelete == v) {

			if (delete(mCurrentPath + File.separator + mCurrentFile)) {
				finish();
			} else {

				Toast.makeText(this, R.string.msg_delete_file_error, 1000)
						.show();
			}
			// Log.d(TAG, "delete file" + f.getAbsoluteFile());
		} else if (mBtnCut == v) {
			Intent i = new Intent();
			App.cutFile = mCurrentPath + File.separator + mCurrentFile;
			i.putExtra(CURRENT_FILE, App.cutFile);
			this.setResult(OPERATOR_CUT, i);
			finish();

		} else if (mBtnCopy == v) {
			Intent i = new Intent();
			App.cutFile = mCurrentPath + File.separator + mCurrentFile;
			i.putExtra(CURRENT_FILE, App.cutFile);
			this.setResult(OPERATOR_COPY, i);

			finish();
		} else if (mBtnPaste == v) {
			pasteOfCut();
			finish();
		}else if(mBtnBookmark == v){
			addToBookmark(mCurrentPath , mCurrentFile);
			finish();
		}

	}

	private void pasteOfCut() {
		if (App.cutFile == null) {
			Toast.makeText(this, R.string.msg_paste_empty_file, 1000).show();
			return;
		} else {
			int index = App.cutFile.lastIndexOf(File.separator);
			String renameFile = App.cutFile.substring(index);
			renameFile = mCurrentPath + File.separator + renameFile;
			Log.d(TAG, "past to:" + renameFile);
			File old = new File(App.cutFile);
			File newF = new File(renameFile);
			if (newF.exists()) {
				Toast.makeText(this, R.string.msg_paste_file_exists, 1000)
						.show();
				return;
			}
			if (!old.exists()) {
				Toast.makeText(this, R.string.msg_paste_file_not_exists, 1000)
						.show();
				return;
			}

			old.renameTo(newF);
			App.cutFile = null;
		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
	}
	
	public boolean addToBookmark(String path,String name){
		DBUtil dbUtil = new DBUtil(this);
		Bookmark b = new Bookmark();
		b.name = name;
		b.path = path;
		b.type = Bookmark.Types.BOOKMARK;
		dbUtil.insert(b);
		return true;
		
	}

	private boolean delete(String deleteFile) {
		File f = new File(deleteFile);
		boolean result = true;
		if (f.isFile()) {
			// Log.d(TAG, "delete1 : "+f.getAbsoluteFile());
			result = result && f.delete();
		} else {
			String[] children = f.list();
			if (children != null && children.length > 0) {
				for (int j = 0; j < children.length; j++) {
					result = result
							&& delete(deleteFile + File.separator + children[j]);
					result = result && delete(deleteFile);

				}
			} else {
				result = result && f.delete();
				// Log.d(TAG, "delete3 : "+f.getAbsoluteFile());
			}
		}
		return result;
	}

	private void rename() {
		File f = new File(mCurrentPath + File.separator + mCurrentFile);
		Editable tmp = mEditRename.getText();
		File rf = new File(mCurrentPath + File.separator
				+ tmp.toString().trim());
		if (f.renameTo(rf)) {
			finish();
		} else {
			Toast.makeText(this, R.string.msg_rename_file_error, 1000).show();
		}
		Log.d(TAG, "Rename to " + rf.getAbsoluteFile());
	}
}
